LAB 1: Grayscale and RGB Color Images

Instructions: labs are hands-on activites. We guide you step-by-step on doing image processing using Python and OpenCV. Read the description, follow example source code, and conduct experiment using your own images. Exercises marked in red boxes are checkpoints that require submissions. At the end of each lab, you will hand-in a zip file of your working directory. Name the directory using your 7-digit student ID and a lab number: 6088xxx_lab1.

Your submitted working directory must include the following:
  1. Your notebook file (.ipynb). It must be excutable with no bugs. We recommend that for your final check before submission, you restart the kernel and re-run the entire code, i.e. from the menu: Kernel -> Restart & Run All
  2. Your input image files (.jpg)
  3. Your output for every execise (.jpg). Please name it with the lab and the exercise number, e.g. lab1ex1.jpg
ToDo: Create and name (if not already done so) your working directory 6088xxx_lab1. Save all your working image files for Lab1 in this directory: this notebook file, all input and output images. Do the same for every lab that follows.

Working with Black & White Images

Read/write and display an image

Choose your input image and save it to Lab1 directory. Choose an image that is different from the ones used as examples in class and choose one that is not the same as your friends. Be creative and have fun!

In [1]:
import cv2 #import OpenCV
from matplotlib import pyplot as plt #import matplotlib

Load an image in grayscale. OpenCV imread takes two parameters: the name of an image file, and a tag 1 for color, 0 for grayscale, and -1 for unchanged (load the image as is). Then we write it to file.

In [2]:
im = cv2.imread('bird.jpg',0)
cv2.imwrite('bird_BW.jpg',im)
Out[2]:
True

Display an image using matplotlib. The cmap argument indicates the colormap. Default is color; ours is grayscale.

In [3]:
plt.imshow(im, cmap='gray')
plt.show()

Explore the properties of the image

In [4]:
print(im.shape)  #Get the dimension of an image height*width
print(im.dtype)  #Get the data type of an image
(900, 900)
uint8

Our bird image is a 2D array of 900 rows and 900 columns, representing the height and width, respectively. Your image may be of different dimensions and that is okay. Each pixel value is stored in an 8-bit unsigned integer (uint8) for color/intensity scaling in the range of 0 to 255.

In most image processing programming, images are stored in multi-dimensional arrays: 2D for grayscale and 3D for color. Operations on images are thus nothing more than mathematical functions performed on arrays and matrices. In Python, these are supported by the NumPy library. We show a couple examples below.

In [5]:
import numpy as np #import numpy library
In [6]:
print(np.min(im))  #Find the minimum pixel value in an image
print(np.max(im))  #Find the maimum pixel value in an image
0
255

Working with Color Images

BGR versus RGB channels

First, we reload our bird image. This time, load it in color (using imread with flag=1). Then, display the color image.

In [7]:
im = cv2.imread('bird.jpg',1)
plt.imshow(im)
plt.show()

What has happened to your colorful image? The colors are wrong! OpenCV orders the three color channels as Blue-Green-Red or BGR. However, matplotlib uses different ordering: Red-Greed-Blue or RGB. Since we read an image using OpenCV, our image is BGR. To correctly display the image, we thus have to convert from BGR to RGB before we give it to matplotlib for display.

In [8]:
plt.imshow(cv2.cvtColor(im,cv2.COLOR_BGR2RGB))
plt.show()

Explore the information in each color channel

Next, let's try to analze the information contained in each of the three channels individually. First, we separate the three color channels into three layers. These channels correspond to the third dimension of our image array. Since our image is BGR, the first layer (index 0) is blue, then green and red. Each layer can be displayed as a 2D grayscale image, whose intensity indicates the amount of the color a pixel contains.

Notice: how to layout multiple images in a single figure, how to hide both axes, how to assign title/label, and how to save a result to file. We note here that you will need to adjust figsize and fontsize to fit with your own image.
In [9]:
fig, axes = plt.subplots(2, 3, figsize=(30,20))
plt.tight_layout()

axes[0,0].imshow(cv2.cvtColor(im,cv2.COLOR_BGR2RGB))
axes[0,0].set_title("Original", fontsize=32)
axes[0,0].axis('off')
axes[0,1].imshow(im)
axes[0,1].set_title("!!!Incorrect channels!!!", fontsize=32)
axes[0,1].axis('off')
axes[0,2].imshow(cv2.cvtColor(im,cv2.COLOR_BGR2GRAY), cmap='gray')
axes[0,2].set_title("8-Bit Grayscale", fontsize=32)
axes[0,2].axis('off')

axes[1,0].imshow(im[:,:,0], cmap='gray')
axes[1,0].set_title("Blue", fontsize=32)
axes[1,0].axis('off')

axes[1,1].imshow(im[:,:,1], cmap='gray')
axes[1,1].set_title("Green", fontsize=32)
axes[1,1].axis('off')

axes[1,2].imshow(im[:,:,2], cmap='gray')
axes[1,2].set_title("Red", fontsize=32)
axes[1,2].axis('off')

plt.show()
fig.savefig("Lab1Ex1.jpg", bbox_inches='tight')

Observing the results, in the blue layer, the body of both birds are black indicating that there is no blue in these regions. On the other hand, the wings of both birds have a brighter shade of gray because they are blue.

Try this: explain why in the red channel the body of the bird on the right (which is yellow) is more white than the head of the left bird (which is of red color). How about the green channel; what can you say about it?
Exercise #1: BGR channel in grayscale. Follow the sample source code above and complete the same set of operations on your own image. Then, display your results (a total of six images) using the same layout.
ToDo: in this and every exercise that follows, along with the instructions, we provide example output. Be sure to arrange your results in the same ordering and format as shown in example output, and do not forget to label them. Hint: check your own work, look at your results and observe their colors. Do they match your expectation?

Coloring and mixing up the channels

Give the blue channel the blue color. Create a copy of our image. Instead of extracting the blue as we did earlier, we leave it alone. We then set the other two channels to zero.

In [10]:
onecolorB = im.copy()
onecolorB[:,:,1] = 0
onecolorB[:,:,2] = 0
Try this: display the image onecolorB. How does it look? What are the similarities or differences between this image and the blue channel in grayscale that we plot earlier? We show you our results here; you do yours.
Exercise #2: One-channel color image. Above, we have created one one-color image for the blue channel. Your task is to create the other two one-color images for the green and red channels. Then, display the three channels side-by-side. An example of an expected output is shown below.
Exercise #3: Two-channel color image. Pairing of the BGR channels. Create and display three two-channel color images; they are blue-green, green-red, and red-blue. An example of an expected output is shown below. Hint: to display one-color image, we set the other two channels to zeros. Thus, to display two-color image, which channel or channels should be set to zeros?
READY FOR SUBMISSION? Check your working directory, 6088xxx_lab1, it should now contained the following files. Please zip the folder and submit it to MyCourse.